<?php

defined('SYSPATH') or die('Access error!');

class Model_User_Group extends Model
{

    public $rules = array(
        'name' => array
            (
            'not_empty' => null,
        ),
        'en_name' => array
            (
            'not_empty' => null,
        )
    );

    /**
     *  获取用户组列表
     * @param type $where
     * @return type
     */
    public function get_group_list($where = '')
    {
        $where = !empty($where) ? ' AND ' . $where : '';
        $where .= ' ORDER BY experience,position,id DESC';
        $group_list_temp = $this->get_results($where);
        $group_list = array();
        if (count($group_list_temp) > 0)
        {
            foreach ($group_list_temp as $key => $value)
            {
                $value['count_user'] = $this->get_count_user($value['id']);
                $group_list[] = $value;
            }
            return count($group_list) > 0 ? $group_list : array();
        }
        return array();
    }

    /**
     *  获取某一个用户组下的用户数量
     * @param type $group_id
     * @return type
     */
    public function get_count_user($group_id)
    {
        $user = Model_User::factory('user');
        $user_count = $user->get_var(array('group_id' => $group_id), 'COUNT(*)');
        return !empty($user_count) ? $user_count : 0;
    }

    /**
     *  转移用户组
     * @param type $from_id
     * @param type $to_id
     * @return type
     */
    public function transfer_user_group($from_id, $to_id)
    {
        $user = Model_User::factory('user');
        $transfer_flag = $user->update(array('group_id' => $to_id), array('group_id' => $from_id));
        return $transfer_flag;
    }

    /**
     *  获取用户组的下拉列表
     */
    public function select_group_list()
    {
        $group_list = $this->get_group_list();
        $html = "";
        if (count($group_list) > 0)
        {
            foreach ($group_list as $value)
            {
                $html .= "<option value='" . $value['id'] . "'>" . $value['name'] . "</option>";
            }
        }
        return $html;
    }

    public function get_usergroup_by_id($id, $param = '*')
    {
        $group_detail = $this->get_row(array('id' => $id), $param);
        if (count($group_detail) > 0)
        {
            if (!empty($group_detail['icon']))
            {
                $group_detail['icon'] = USER_ICON . $group_detail['icon'];
            }
            $group_detail['privilege_list'] = $this->get_group_privilege_id($id);
        }
        return count($group_detail) ? $group_detail : array();
    }

    public function get_group_privilege_id($group_id)
    {
        $id = intval($group_id);
        $sql = "SELECT privilege_id FROM " . $this->db->get_table('user_group_privilege') . " WHERE group_id=" . $id;
        $privilege_list = $this->db->get_col($sql, 0);
        return Arr::validate_arr($privilege_list) ? $privilege_list : array();
    }

    public function get_group_privilege($data = array())
    {
        $where = ' WHERE 1=1 ';
        if (isset($data['en_name']))
        {
            $where .=" AND g.en_name='" . $data['en_name'] . "'";
        }
        $sql = "SELECT up.name,up.module " .
                "FROM " . $this->table . " AS g " .
                " LEFT JOIN " . $this->db->get_table('user_group_privilege') . " AS gp ON gp.group_id = g.id " .
                " LEFT JOIN " . $this->db->get_table('user_privilege') . " AS up ON up.id=gp.privilege_id " . $where;
        $group_privilege = $this->db->get_results($sql, ARRAY_A);

        return Arr::validate_arr($group_privilege) ? $group_privilege : array();
    }

    public function save_group_privilege($data)
    {
        if (isset($data['group_id']))
        {
            $group_id = intval($data['group_id']);
            if ($group_id != 0)
            {
                $sql = "DELETE FROM " . $this->db->get_table('user_group_privilege') . " WHERE group_id=" . $group_id;
                $this->db->query($sql);
            }
        }

        if ($group_id != 0 && isset($data['privilege_id']) && Arr::validate_arr($data['privilege_id']))
        {
            $privilege_list = $data['privilege_id'];
            foreach ($privilege_list as $key => $value)
            {
                if (!empty($value))
                {
                    $sql = " INSERT INTO " . $this->db->get_table('user_group_privilege') . " SET privilege_id=" . $value . ",group_id=" . $group_id;
                    $this->db->query($sql);
                }
            }
        }
    }

}